package cn.tedu.erp.project.service.impl;

import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.mapper.WareHouseCategoryMapper;
import cn.tedu.erp.project.pojo.dto.WareHouseCategoryAddNewDTO;
import cn.tedu.erp.project.pojo.dto.WareHouseCategoryUpdateDTO;
import cn.tedu.erp.project.pojo.entity.WareHouseCategory;
import cn.tedu.erp.project.pojo.vo.WareHouseCategoryListVO;
import cn.tedu.erp.project.pojo.vo.WareHouseCategoryStandardVO;
import cn.tedu.erp.project.service.IWareHouseCategoryService;
import cn.tedu.erp.project.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class WareHouseCategoryServiceImpl implements IWareHouseCategoryService {
    
    @Autowired
    private WareHouseCategoryMapper mapper;
    
    @Override
    public void addNew(WareHouseCategoryAddNewDTO wareHouseCategoryAddNewDTO) {
        log.debug("开始处理【添加商品】的业务，参数：{}", wareHouseCategoryAddNewDTO);
        // 调用参数对象的getName()得到尝试添加的商品的名称
        String name = wareHouseCategoryAddNewDTO.getName();
        // 调用Mapper对象的countByName()执行统计查询
        int count = mapper.countByName(name);
        log.debug("根据名称【{}】统计数量，结果：{}", name, count);
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "添加商品失败，商品名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建WareHouseCategory对象
        WareHouseCategory wareHouseCategory = new WareHouseCategory();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到WareHouseCategory对象中
        BeanUtils.copyProperties(wareHouseCategoryAddNewDTO, wareHouseCategory);
        // 调用Mapper对象的insert()执行插入商品数据
        log.debug("即将执行插入数据，参数：{}", wareHouseCategory);
        mapper.insert(wareHouseCategory);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除商品】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        WareHouseCategoryStandardVO wareHouseCategory = mapper.getStandardById(id);
        log.debug("根据ID={}检查商品数据是否存在，查询结果：{}", id, wareHouseCategory);
        // 判断查询结果是否为null
        if (wareHouseCategory == null) {
            // 是：数据不存在，抛出异常
            String message = "删除商品失败，尝试删除的商品数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        mapper.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, WareHouseCategoryUpdateDTO wareHouseCategoryUpdateDTO) {
        log.debug("开始处理【修改商品详情】的业务，ID：{}，新数据：{}", id, wareHouseCategoryUpdateDTO);
        // 调用Mapper对象的getStandardById()执行查询
        WareHouseCategoryStandardVO queryResult = mapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改商品详情失败，尝试修改的商品数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的countByNameAndNotId()执行统计
        int count = mapper.countByNameAndNotId(id, wareHouseCategoryUpdateDTO.getName());

        // 创建WareHouseCategory对象
        WareHouseCategory wareHouseCategory = new WareHouseCategory();
        // 复制属性，设置ID
        BeanUtils.copyProperties(wareHouseCategoryUpdateDTO, wareHouseCategory);
        wareHouseCategory.setId(id);
        // 调用Mapper对象的update()方法执行修改
        mapper.update(wareHouseCategory);
    }

    @Override
    public WareHouseCategoryStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询商品详情】的业务，参数：{}", id);
        // WareHouseCategoryStandardVO queryResult = mapper.getStandardById(id);
        WareHouseCategoryStandardVO queryResult = mapper.getStandardById(id);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "查询商品详情失败，商品数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<WareHouseCategoryListVO> list() {
        log.debug("开始处理【查询商品列表】的业务，无参数");
        // List<WareHouseCategoryListItemVO> list = mapper.list();
        List<WareHouseCategoryListVO> list = mapper.list();
        return list;
    }
}
